package com.tcs.teg.web.accounts;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.tcs.teg.domain.accounts.Account;
import com.tcs.teg.domain.accounts.Beneficiary;



public class AccountValidator implements Validator {
	
	public boolean supports(Class clazz) {
        return Account.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
    	Account account = (Account) target;
    	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",  "empty.value.account.name");
    	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contactNumber", "empty.value.account.number");
    	
    	//Validating the beneficiaries
    	List<Beneficiary> beneficiaries = account.getBeneficiaries();
    	int size = beneficiaries.size(); 
    	for(int i=0;i< size; i++){
    		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "beneficiaries["+i+"].name",  "empty.value.account.beneficiaries.name");
    		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "beneficiaries["+i+"].allocationPercentage",  "empty.value.account.beneficiaries.allocationPercentage");
    	}
    	
    			
    	
    }
}

